package com.cruel_wandering.wifi_geo_predictor;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;


public class CellID {
	// REQUIRE INTERNET CONNECTION !!!!
	private static final String TAG = "CellID";
	private Context ctx;
    private GsmCellLocation location;
    private int cellID, lac;
    private double lng, lat;
    
    public CellID(Context ctx)
    {
    	this.ctx = ctx;
        TelephonyManager tm  = 
            (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE); 
        location = (GsmCellLocation) tm.getCellLocation();
        cellID = location.getCid();
        lac = location.getLac();
        try {
			computeLocation();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    
    
    private void computeLocation() throws Exception 
    {
        String urlString = "http://www.google.com/glm/mmap";            
    
        //---open a connection to Google Maps API---
        URL url = new URL(urlString); 
        URLConnection conn = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) conn;        
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true); 
        httpConn.setDoInput(true);
        httpConn.connect(); 
        
        //---write some custom data to Google Maps API---
        OutputStream outputStream = httpConn.getOutputStream();
        WriteData(outputStream);       
        
        //---get the response---
        InputStream inputStream = httpConn.getInputStream();  
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        
        //---interpret the response obtained---
        dataInputStream.readShort();
        dataInputStream.readByte();
        int code = dataInputStream.readInt();
        if (code == 0) {
            lat = (double) dataInputStream.readInt() / 1000000D;
            lng = (double) dataInputStream.readInt() / 1000000D;
        }else{
        	String msg = "There was an error during computation of location by CellID";
        	Logger.getInstance(ctx).log(TAG, msg);
        }

    } 
    
    
    private void WriteData(OutputStream out) 
    throws IOException
    {    	
        DataOutputStream dataOutputStream = new DataOutputStream(out);
        dataOutputStream.writeShort(21);
        dataOutputStream.writeLong(0);
        dataOutputStream.writeUTF("en");
        dataOutputStream.writeUTF("Android");
        dataOutputStream.writeUTF("1.0");
        dataOutputStream.writeUTF("Web");
        dataOutputStream.writeByte(27);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(3);
        dataOutputStream.writeUTF("");

        dataOutputStream.writeInt(cellID);  
        dataOutputStream.writeInt(lac);     

        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.writeInt(0);
        dataOutputStream.flush();    	
    }
    
    
    
    private double getLatitude (){
    	return lat;
    }

    private double getLongitude (){
    	return lng;
    }
    
    public Location getLocation(){
    	Location loc = new Location(TAG);
    	loc.setLatitude(getLatitude());
    	loc.setLongitude(getLongitude());
    	return loc;
    }
    
}
